package springapp.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import springapp.util.Utils;

@Entity
@Table(name = "Company")
public class CompanyEntity extends CloneableEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Integer companyId;

	@Column(name = "company_name")
	private String companyName;

	@Column(name = "hq")
	private String headquarters;

	@Column(name = "rating")
	private Float rating = (float) 0;

	@Column(name = "industry_id")
	private Integer industryId;

	@Transient
	private String industryName;

	/**
	 * The below values need to be primed
	 */
	@Transient
	private int numReviews = 0;

	@Transient
	private int numSalaries = 0;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "COMPANY_LOCATION", joinColumns = { @JoinColumn(name = "COMPANY_ID") }, inverseJoinColumns = { @JoinColumn(name = "LOCATION_ID") })
	private Set<LocationEntity> locations = new HashSet<LocationEntity>(0);

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "COMPANY_DESIGNATION", joinColumns = { @JoinColumn(name = "COMPANY_ID") }, inverseJoinColumns = { @JoinColumn(name = "DESIGNATION_ID") })
	private Set<DesignationEntity> designations = new HashSet<DesignationEntity>(
			0);

	public CompanyEntity(Integer companyId, String companyName,
			String headquarters) {
		super();
		this.companyId = companyId;
		this.companyName = companyName;
		this.headquarters = headquarters;
	}

	public CompanyEntity(CompanyEntity companyEntity) {
		this(companyEntity.getCompanyId(), companyEntity.getCompanyName(),
				companyEntity.getHeadquarters());
	}

	public CompanyEntity() {
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getHeadquarters() {
		return headquarters;
	}

	public void setHeadquarters(String headquarters) {
		this.headquarters = headquarters;
	}

	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Company Id").append(this.companyId);
		builder.append("Company name").append(this.companyName);
		builder.append(" Hq").append(this.headquarters);
		return builder.toString();
	}

	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}

		if (object != null && object instanceof CompanyEntity) {
			CompanyEntity entity = (CompanyEntity) object;
			if (Utils.isEqual(entity.companyId, this.companyId)) {
				return true;
			}
		}

		return false;
	}

	@Override
	public int hashCode() {
		return this.companyId.hashCode();
	}

	public Float getRating() {
		return rating;
	}

	public void setRating(Float rating) {
		this.rating = rating;
	}

	public Integer getIndustryId() {
		return industryId;
	}

	public void setIndustryId(Integer industryId) {
		this.industryId = industryId;
	}

	public Set<LocationEntity> getLocations() {
		return locations;
	}

	public void setLocations(Set<LocationEntity> locations) {
		this.locations = locations;
	}

	public Set<DesignationEntity> getDesignations() {
		return designations;
	}

	public void setDesignations(Set<DesignationEntity> designations) {
		this.designations = designations;
	}

	public String getIndustryName() {
		return industryName;
	}

	public void setIndustryName(String industryName) {
		this.industryName = industryName;
	}

	public int getNumReviews() {
		return numReviews;
	}

	public void setNumReviews(int numReviews) {
		this.numReviews = numReviews;
	}

	public int getNumSalaries() {
		return numSalaries;
	}

	public void setNumSalaries(int numSalaries) {
		this.numSalaries = numSalaries;
	}
}
